package com.tsxy.service.impl;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tsxy.common.Constants;
import com.tsxy.controller.administrator.BookSortController;
import com.tsxy.entity.Book;
import com.tsxy.entity.BookSort;
import com.tsxy.entity.Order;
import com.tsxy.entity.OrderDetails;
import com.tsxy.mapper.BookMapper;
import com.tsxy.mapper.BookSortMapper;
import com.tsxy.mapper.OrderDetailsMapper;
import com.tsxy.mapper.OrderMapper;
import com.tsxy.service.BookSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BookSortServiceImpl implements BookSortService {
    @Autowired
    private BookSortMapper bookSortMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private BookMapper bookMapper;
    @Override
    public List<BookSort> getAll() {
        return bookSortMapper.getAll();
    }

    @Override
    public Map<String, List<Integer>> getNum(HashMap<String, List<Integer>> rela) {
        List<Order> orders = orderMapper.selectList(new QueryWrapper<>());
        Map<String,List<Integer> > k = new HashMap();
        orders.stream().forEach(order -> {
            //创建map 用来存储 分类对应的书的数量
            HashMap<String, Integer> map = new HashMap<>();
            List<OrderDetails> orderDetails = orderDetailsMapper.selectList(new QueryWrapper<OrderDetails>().eq("order_id", order.getId()));
            orderDetails.stream().forEach(orderDetails1 -> {
                Book book = bookMapper.selectOne(new QueryWrapper<Book>().eq("id", orderDetails1.getBookId()));
                orderDetails1.setBook(book);
                for (Map.Entry<String, List<Integer>> entry : rela.entrySet()) {
                    if ( entry.getValue() != null && book != null){
                        if(entry.getValue().contains(book.getBookSortId() ) ){
                            if (map.containsKey(entry.getKey())){
                                Integer value = map.get(entry.getKey()) + orderDetails1.getCount();
                                // 算出 类型 对应的这个订单的
                                map.put(entry.getKey(),value);
                            }else {
                                map.put(entry.getKey(),orderDetails1.getCount());
                            }
                        }
                    }
                }
            });
            order.setOrderDetails(orderDetails);
            Month month = order.getTime().getMonth();
            Integer index = Constants.months.get(month.toString().toUpperCase());
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                if (k.containsKey(entry.getKey())){
                    List<Integer> list = k.get(entry.getKey());
                    Integer integer = list.get(index - 1);
                    integer += entry.getValue();
                    list.set(index - 1 ,integer);
                }else {
                    List<Integer> list = new ArrayList<>();
                    for (int i = 0 ; i < 12; i ++){
                        list.add(0);
                    }
                    Integer value = entry.getValue();
                    list.set(index - 1 ,value);
                    k.put(entry.getKey(),list);
                }
            }
        });
        return k;
    }
}
